我不明白我的代码有什么问题。在某些设备中,我的相机分辨率很好。但在某些设备上,我的相机分辨率并不好。我在StackOverflow上搜索了许多解决方案,但它们都没有为我工作。是否有人面临这种情况。给我个人提供建议 这是我的班级。
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {
ImageView capture_image_button, camera_image_view, imagecancel, cameraClose;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera.ShutterCallback myShutterCallback;
Camera.PictureCallback myPictureCallback_RAW;
//Bitmap correctBmp;
Camera.PictureCallback myPictureCallback_JPG;
Button uploadImage;
private ImageHolder imageHolder;
public String path;
int mVinappserial;
static FetchDocument fetchDocument;
UtilityOfActivity utilityOfActivity;
File file;
Bitmap bm;
boolean previewing = false;
public final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
LayoutInflater controlInflater = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
this, Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
myShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
}
};
myPictureCallback_RAW = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageInByte, Camera arg1) {
}
};
myPictureCallback_JPG = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageInByte, Camera arg1) {
if (imageInByte != null) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
//int screenHeight = getResources().getDisplayMetrics().heightPixels;
bm = BitmapFactory.decodeByteArray(imageInByte, 0, (imageInByte != null) ? imageInByte.length : 0);
float ratio = (float)bm.getWidth() / (float)bm.getHeight();
int screenHeight = (int)(ratio*screenWidth);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
bm = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
}else{// LANDSCAPE MODE
//No need to reverse width and height
Bitmap scaled = Bitmap.createScaledBitmap(bm, screenWidth,screenHeight , true);
bm=scaled;
}
// photoPreview.setImageBitmap(bm);
}
// isImageCaptured = true;
//photoPreview.setVisibility(View.VISIBLE);
// surfaceView.setVisibility(View.GONE);
//Bitmap bitmapPicture = BitmapFactory.decodeByteArray(imageInByte, 0, imageInByte.length);
/* int orientation = Exif.getOrientation(imageInByte);
// Bitmap bmp = BitmapFactory.decodeByteArray(imageInByte, 0, bytes.length);
switch (orientation) {
case 90:
bitmapPicture = UtilityOfActivity.rotateImage(bitmapPicture, 90);
break;
case 180:
bitmapPicture = UtilityOfActivity.rotateImage(bitmapPicture, 180);
break;
case 270:
bitmapPicture = UtilityOfActivity.rotateImage(bitmapPicture, 270);
break;
case 0:
// if orientation is zero we don't need to rotate this
default:
break;
}*/
if (bm != null) {
file = new File(Environment.getExternalStorageDirectory() + "/myDoc");
if (!file.isDirectory()) {
file.mkdir();
}
file = new File(Environment.getExternalStorageDirectory() + "/myDoc",
System.currentTimeMillis() + ".jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
// bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
bm.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/* try {
bitmapPicture = UtilityOfActivity.rotateImageIfRequired(bitmapPicture, file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}*/
//bm = Bitmap.createBitmap(bm, 0, 0, 76, 100, null, true);
// correctBmp = UtilityOfActivity.getResizedBitmap(correctBmp, 1000);
byte[] byteImage = UtilityOfActivity.bitmaptoByteArray(bm);
imageHolder = new ImageHolder();
if (byteImage != null) {
imageHolder.setImageInByte(byteImage);
}
imageHolder.setImageName(file.getName());
imageHolder.setIsUpload(0);
imageHolder.setImagePath(file.getAbsolutePath());
Log.d("", "");
camera_image_view.setVisibility(View.VISIBLE);
imagecancel.setVisibility(View.VISIBLE);
camera_image_view.setImageBitmap(Bitmap.createScaledBitmap(bm, 120, 150, false));
capture_image_button.setEnabled(false);
capture_image_button.setAlpha(0.3f);
utilityOfActivity.progresDissmiss();
}
};
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.custom, null);
capture_image_button = (ImageView) viewControl.findViewById(R.id.capture_image_button);
camera_image_view = (ImageView) viewControl.findViewById(R.id.camera_image_view);
imagecancel = (ImageView) viewControl.findViewById(R.id.imagecancel);
cameraClose = (ImageView) viewControl.findViewById(R.id.cameraClose);
uploadImage = (Button) viewControl.findViewById(R.id.uploadImage);
capture_image_button.setOnClickListener(this);
uploadImage.setOnClickListener(this);
imagecancel.setOnClickListener(this);
cameraClose.setOnClickListener(this);
if (getIntent() != null) {
fetchDocument = getIntent().getParcelableExtra(Constant.KEY_OBJECT);
mVinappserial = getIntent().getIntExtra(Constant.KEY_VINAPPSERIAL, 0);
}
utilityOfActivity = new UtilityOfActivity(CameraActivity.this);
ActionBar.LayoutParams layoutParamsControl = new ActionBar.LayoutParams(ActionBar.LayoutParams.FILL_PARENT,
ActionBar.LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Camera.Parameters params = camera.getParameters();
if (previewing) {
camera.stopPreview();
previewing = false;
}
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
//params.setRotation(270);//Just add this single line of code***
} else {
camera.setDisplayOrientation(0);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
setCameraDisplayOrientation(this, Camera.CameraInfo.CAMERA_FACING_BACK, camera);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.capture_image_button:
utilityOfActivity.progressDialogShow("please wait...");
if (camera != null) {
camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
}
break;
case R.id.uploadImage: {
saveImage();
break;
}
case R.id.imagecancel:
Intent intent = new Intent(CameraActivity.this,
CameraActivity.class);
intent.putExtra(Constant.KEY_OBJECT, fetchDocument);
intent.putExtra(Constant.KEY_VINAPPSERIAL, mVinappserial);
startActivity(intent);
finish();
case R.id.cameraClose:
finish();
break;
}
}
public void saveImage() {
if (imageHolder != null) {
WebRequest webRequest = new WebRequest(this);
SaveImageRequest saveImageRequest = new SaveImageRequest();
saveImageRequest.setCalledBy(Constant.Called_by_INSERT);
saveImageRequest.setDocSerial(Constant.DOC_SERIAL_PROFILE_0);
saveImageRequest.setNewFileName(imageHolder.getImageName());
saveImageRequest.setImageData(imageHolder.getImageInByte());
saveImageRequest.setImagePath(imageHolder.getImagePath());
saveImageRequest.setDocCode(fetchDocument.getDocTypeCode());
saveImageRequest.setFetchDocument(fetchDocument);
saveImageRequest.setVehSerial(mVinappserial);
webRequest.uploadImageDocument(saveImageRequest, CameraActivity.this);
// this.getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Intent intent=getIntent();
// this.finish();
} else {
utilityOfActivity.toast(getString(R.string.please_select_image));
}
}
public void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
public void setCamera(Camera camera) {
this.camera = camera;
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
camera.setParameters(parameters);
}
}
我对任何帮助都很满意。 谢谢你提前。