在我的应用程序对自定义相机的要求中,因此开发了一款,到目前为止一切正常,也可以拍摄照片,但是当我在横向模式下使用三星 Galaxy ace android 手机(2.3)拍照时,它还可以,但是在进入纵向模式时,视图图片以 90 度角移动到左侧。正如您在我的代码中看到的那样,我也尝试更改表面位置,但没有任何反应。这是 Myactivity 类的代码
package com.goutam.test_camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class CameraclassActivity extends Activity implements OnClickListener,
PictureCallback {
ImageButton take_picture, retake_button, save_picture, setting_button;
FrameLayout camerapreviewsurface;
Previewclass cameradisplayclass;
ImageView capture_image;
HorizontalScrollView myscrollview;
private Camera myowncamera;
byte[] finalimage;
Bitmap pic_cap_bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_portrait);
take_picture = (ImageButton) findViewById(R.id.imageButton1);
//
retake_button = (ImageButton) findViewById(R.id.imageButton3);
//
save_picture = (ImageButton) findViewById(R.id.imageButton4);
//
setting_button = (ImageButton) findViewById(R.id.imageButton2);
//
capture_image = (ImageView) findViewById(R.id.imageView1);
//
myscrollview = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
//
take_picture.setOnClickListener(this);
retake_button.setOnClickListener(this);
save_picture.setOnClickListener(this);
//
retake_button.setVisibility(View.INVISIBLE);
save_picture.setVisibility(View.INVISIBLE);
setting_button.setVisibility(View.INVISIBLE);
capture_image.setVisibility(View.INVISIBLE);
myscrollview.setVisibility(View.INVISIBLE);
//
myowncamera = Camera.open();
camerapreviewsurface = (FrameLayout) findViewById(R.id.framelayout1);
cameradisplayclass = new Previewclass(this, myowncamera);
camerapreviewsurface.addView(cameradisplayclass);
Log.i("ACTIVITY", "ON CREATE");
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageButton1:
takePicture();
break;
}
}
private void takePicture() {
// TODO Auto-generated method stub
take_picture.setVisibility(View.INVISIBLE);
cameradisplayclass.takepicture(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
// setContentView(R.layout.layout_portrait);
super.onConfigurationChanged(newConfig);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
finalimage = data;
// camera.stopPreview();
this.myowncamera = camera;
retake_button.setVisibility(View.INVISIBLE);
camerapreviewsurface.setVisibility(View.INVISIBLE);
setting_button.setVisibility(View.VISIBLE);
capture_image.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "PICTURE TAKEN", 0).show();
new DisplayImageTask().execute(finalimage);
}
private class DisplayImageTask extends AsyncTask<byte[], Void, Bitmap> {
@Override
protected Bitmap doInBackground(byte[]... parameter) {
pic_cap_bitmap = BitmapFactory.decodeByteArray(finalimage, 0,
finalimage.length);
return pic_cap_bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
capture_image.setImageBitmap(pic_cap_bitmap);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
Toast.makeText(getApplicationContext(), "DISPLAYING IMAGE", 0)
.show();
super.onProgressUpdate(values);
}
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.layout_portrait, menu);
// return true;
@Override
protected void onPause() {
Log.i("ACTIVITY", "ON PAUSE");
System.gc();
this.finish();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
System.gc();
this.finish();
myowncamera.release();
super.onBackPressed();
}
@Override
protected void onDestroy() {
this.finish();
System.gc();
super.onDestroy();
}
}
这是我的surfaceholder的预览类的代码来保存表面视图
package com.goutam.test_camera;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class Previewclass extends SurfaceView implements Callback {
private Camera mcamera;
boolean ispreviewing = false;
// WindowManager manager;
SurfaceHolder holder;
public Previewclass(Context context, Camera mycamera) {
super(context);
this.mcamera = mycamera;
// manager=(WindowManager)
// context.getSystemService(Context.WINDOW_SERVICE);
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// mcamera=null;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("CAMERA", "ON SURFACE CHANGE");
try {
mcamera.setPreviewDisplay(this.holder);
mcamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if(ispreviewing==true)
// {
// mcamera.stopPreview();
// }
// Parameters mparameters=mcamera.getParameters();
// Display display=manager.getDefaultDisplay();
// if(display.getRotation()==Surface.ROTATION_0)
// {
// mparameters.setPreviewSize(height, width);
// mcamera.setDisplayOrientation(90);
// }
// if(display.getRotation()==Surface.ROTATION_90)
// {
// mparameters.setPreviewSize(width, height);
// }
// if(display.getRotation() == Surface.ROTATION_180)
// {
// mparameters.setPreviewSize(height, width);
// }
// if(display.getRotation() == Surface.ROTATION_270)
// {
// mparameters.setPreviewSize(width, height);
// mcamera.setDisplayOrientation(180);
// }
// mcamera.setParameters(mparameters);
// previewCamera();
}
// private void previewCamera()
// {
// try {
// mcamera.setPreviewDisplay(this.holder);
// mcamera.startPreview();
// ispreviewing = true;
// } catch (IOException e) {
// Log.e("CAMERA DISPLAY", "ERROR");
// e.printStackTrace();
// }
//
// }
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("CAMERA", "ON SURFACE CREATED");
try {
mcamera.setPreviewDisplay(holder);
ispreviewing = true;
mcamera.startPreview();
} catch (IOException e) {
Log.e("CAMERA OPENING", "ERROR");
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("CAMERA", "ON SURFACE DESTROY");
this.mcamera = null;
// this.manager=null;
this.holder = null;
// mcamera.stopPreview();
// this.mcamera.release();
}
public void takepicture(PictureCallback imageCallback) {
mcamera.takePicture(null, null, imageCallback);
}
}
这是我的 androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goutam.test_camera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.goutam.test_camera.CameraclassActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我被这件事困扰了很长时间,但没有找到任何解决方案,因为我是 android 新手,无法识别 android 设备上的行为。但有一件事是相同的代码在 htc 制造的 android 手机上工作得很好。所以,我可以假设这是制造商提供的硬件默认设置。如果有人想要 .apk 文件进行测试,我可以发送到他们。电子邮件 id:gkundu07@gmail.com 对不起我的语言,感谢您收听我。任何形式的帮助将不胜感激。