5
こんにちは。私はカメラコードを使用するAndroidアプリケーションを作成しています。私はandroid-API 2.1を使用しています。アプリケーションをdell mobileにインストールして、カメラの向きが正しい。しかし、私はサムスンギャラクシーS2などの他のアンドロイド携帯に自分のアプリケーションをインストールしました。ここでは、カメラの向きに問題があります。ポートレートモードでもカメラは90度回転します。誰もがカメラのパラメータがすべての携帯電話でうまく動作するように変更する必要がある場合は、私にお勧めできますか?ここに私のコードは次のとおりです。Androidでのカメラの向き
//class CameraDemo
public class CameraDemo extends Activity {
private static final String TAG = "CameraDemo";
Camera camera;
Preview preview;
Button buttonClick;
Button next;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.camdemo);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
buttonClick = (Button) findViewById(R.id.buttonClick);
next = (Button) findViewById(R.id.next);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
try{
Constants.takepic=1;
buttonClick.setEnabled(false); //once click is clicked, its disable
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
if(Constants.takepic==1)
{
Intent intent_new=new Intent(CameraDemo.this,NotesAndUpload.class);
startActivity(intent_new);
}
else
{
Toast.makeText(CameraDemo.this, "Take a picture",5);
}
}
});
Log.d(TAG, "onCreate'd");
}
catch(Exception e){}
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//taking time and date
Constants.dateValue=dateAndTime("yyyy-MM-dd");
Constants.timeValue=dateAndTime("HH:mm:ss");
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
CameraDemo.this.openFileOutput(String.format("%d.j pg",
System.currentTimeMillis()), 0);
// Or write to sdcard
long imageNameLong=System.currentTimeMillis();
Constants.imageName=new Long(imageNameLong).toString();
outStream = new
FileOutputStream(String.format("/sdcard/%d.jpg",imageNameLong));
outStream.write(data);
Constants.takepic=1;
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
}
//Preview Class
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BU FFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
/*FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",
System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}*/
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
camera.stopPreview();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
//CAMERA PARAMETERS NEED TO BE CHANGED :-
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
parameters.set("rotation",90);
parameters.set("orientation", "portrait");
}
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("rotation", 90);
parameters.set("orientation", "landscape");
}
parameters.set("jpeg-quality", 50);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p= new Paint(Color.RED);
Log.d(TAG,"draw");
canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p);
}
}
同様のカメラの画面回転の問題に対する私の答えを見てください(http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways/5110406#5110406) あなたの現在の問題。 –